#include "pch.h"

Cannon::Cannon()
{
    x = 0;
    y = GraphicsDevice::sInstance->getScreenHeight() - 147;
    w = 91;
    h = 88;
    rotation = 0;
    power = 0;
    movingUp = true;
    rotating = true;
    shot = false;

    launchState = 0;
    highScore = 0;

    firstCannon = GraphicsDevice::sInstance->load_image("images/angledcannonsmall.png");
    rotatedCannon = NULL;
}

Cannon::~Cannon()
{
    //dtor
}

void Cannon::show_object(Projectile* projectile)
{
    rotatedCannon = firstCannon;
    rotatedCannon = rotozoomSurface(rotatedCannon, rotation, 1, SMOOTHING_ON);
    if (rotatedCannon)
    {
        GraphicsDevice::sInstance->apply_surface(x + (firstCannon->w/2 - rotatedCannon->w/2),
                                                 y + (firstCannon->h/2 - rotatedCannon->h/2), w, h, rotatedCannon, GraphicsDevice::sInstance->getGameScreen());
    }
    if(rotating == true)
    {
        if (FPSCounter::sInstance->getMovingUp())
        {
            rotation = 60 * (FPSCounter::sInstance->update.getTicks() / 1000.f) - 35;
        }
        else
        {
            rotation = (60 - 60 * (FPSCounter::sInstance->update.getTicks() / 1000.f) - 35);
        }
    }

    if (launchState == 2)
    {
        if (projectile->getTrueX() > highScore)
        {
            highScore = projectile->getTrueX();
        }
    }
}

void Cannon::move_object()
{

}

void Cannon::clear_surface()
{
    SDL_FreeSurface(firstCannon);
    SDL_FreeSurface(rotatedCannon);
}

void Cannon::setRotation()
{
    rotating = false;
    launchState = 1;
}

void Cannon::setPower()
{
    if (shot == false)
    {
        if (FPSCounter::sInstance->getMovingUp())
        {
            power = 50 + 200 * (FPSCounter::sInstance->update.getTicks() / 1000.f);
        }
        else
        {
            power = 50 + (200 - 200 * (FPSCounter::sInstance->update.getTicks() / 1000.f));
        }

    }
    if (shot)
    {
        launchState = 2;
    }
    power = 200;
}

void Cannon::runAction(Projectile* projectile)
{
    switch(launchState)
    {
        case 0:
            setRotation();
            break;
        case 1:
            shot = true;
            projectile->shootProjectile(rotation, this);
            break;
    }
}

void Cannon::resetGame()
{
    x = 0;
    y = GraphicsDevice::sInstance->getScreenHeight() - 147;
    w = 91;
    h = 88;
    rotation = 0;
    power = 0;
    movingUp = true;
    rotating = true;
    shot = false;

    launchState = 0;
}
